package com.xy.controller;

import com.xy.dao.QqDao;
import com.xy.pojo.Qq;
import net.mamoe.mirai.Bot;
import net.mamoe.mirai.BotFactory;
import net.mamoe.mirai.utils.BotConfiguration;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;
import java.util.Objects;

/**
 * @author 于振晗
 * @title: controller
 * @description: TODO
 * @date 2022年01月11日
 * @path com.xy
 */
@RestController
public class LoginController {
    @Autowired
    private QqDao qqDao;

    @Value("${workingDir}")
    private String workingDir;

    @GetMapping("/login/{qq}")
    public String login(@PathVariable("qq") Long qq) {
        Qq qq1 = qqDao.qqCount(qq);
        if (Objects.isNull(qq1)) {
            return "不存在的QQ";
        }
        Bot bot = BotFactory.INSTANCE.newBot(qq1.getQq(), qq1.getPassword(), config -> {
            File file = new File(workingDir + qq);
            if (!file.exists()) {
                file.mkdirs();
            }
            config.setWorkingDir(new File(workingDir + qq));
            config.fileBasedDeviceInfo();
            config.setProtocol(BotConfiguration.MiraiProtocol.ANDROID_PAD);
        });
        try {
            bot.login();
        } catch (Exception e) {
            return e.getMessage();
        }
        int row = qqDao.updateQq(qq);
        return "QQ：" + qq + "启用成功";
    }
}
